NVM.TocBot = (function () {
    var __core = NOVEM.Core;
    var __base = NOVEM.Base;
    var __modules = NOVEM.Modules;

    return {
        init: function (selector) {
            if (__core.getSelector(selector, false, false).length < 1) {
                return true;
            }

            __core.loadJS({
                file: "plugins/tocbot.js",
                id: "nvm-tocbot-js",
                jsFolder: true,
            });

            __core.isFuncTrue(function () {
                return typeof tocbot !== "undefined";
            }).then(function (cond) {
                if (!cond) {
                    return false;
                }
                __core.initFunction({ class: 'has-plugin-tocbot', event: 'pluginTocBotReady' });

                selector = __core.getSelector(selector, false);
                if (selector.length < 1) {
                    return true;
                }

                let userOptions;

                if (selector[0].dataset.tocbotOptions != undefined) {
                    userOptions = JSON.parse(
                        selector[0].dataset.tocbotOptions.replace(/'/g, '"')
                    );

                    let articleContent = document.querySelector(userOptions.contentSelector)
                    let selectors = userOptions.headingSelector
                    if (userOptions.ignoreSelector) {
                        selectors = selectors.split(',')
                            .map(function mapSelectors(selector) {
                                return `${selector.trim()}:not(${userOptions.ignoreSelector})`
                            })
                    } try {
                        articleContent.querySelectorAll(selectors).forEach((heading, index) => {
                            heading.id = `h-` + index
                        })
                    } catch (e) {
                        console.warn(`Headers not found with selector: ${selectors}`);
                        return null
                    }
                }
                else {
                    return true;
                }

                tocbot.init(userOptions);
            })
        },
    };
})();